package com.bonc.ioc.controller;

import com.bonc.ioc.base.controller.BaseController;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.dao.UserDao;
import com.bonc.ioc.util.CurrentUser;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author yiwang
 * @Description 用户信息
 * @Date 9:26 2020/8/4
 * @Param
 * @return
 **/

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private CurrentUser currentUser;
    @Autowired
    private UserDao userDao;

    /**
     * @return
     * @Author yiwang
     * @Description 获取建筑物数量
     * @Date 10:20 2020/8/6
     * @Param
     **/
    @GetMapping("getUserInfo")
    public AppReply getUserInfo(HttpServletRequest request) {
        String userName = currentUser.getUserName(request);
        String userId = currentUser.getUserId(request);
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("userName", userName);
        map.put("userId", userId);
//        String token = null;
//        Object UserInfo = null;
//        Cookie[] cookies = request.getCookies();
//        if (cookies != null) {
//            for (int i = 0; i < cookies.length; i++) {
//                String name = cookies[i].getName();
//                if ("token".equals(name)) {
//                    token = cookies[i].getValue();
//                }
//            }
//        }
//        if (token != null && !"".equals(token)) {
//            UserInfo = redisTemplate.opsForValue().get(token);
//        }

        return new AppReply(AppReply.SUCCESS_CODE, AppReply.SUCCESS_MSG, map);
    }

    @GetMapping("getUserStatus")
    public AppReply getUserStatus(HttpServletRequest request) throws Exception {

        String s = sendPost("http://172.16.193.25:9009/cas/jwt/token.do", new LinkedList<>());
        return new AppReply(AppReply.SUCCESS_CODE, AppReply.SUCCESS_MSG, s);
    }

    public String sendPost(String url, List<BasicNameValuePair> list) {
        //创建post请求对象
        HttpPost post = new HttpPost(url);
        try {

            //把参数放入请求对象，，post发送的参数list，指定格式
            post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
            //添加请求头参数
            CloseableHttpClient client = HttpClients.createDefault();
            post.setHeader("referer", "http://172.16.193.15:8001/");
            post.setHeader("Cookie", "JSESSIONID=5785A7B3307ECB15A05448246C254538; refresh_token=RT-9011-ZCECJpfp9g00ltPKuVbLOedYB3ge2VRztkw; ssoToken=AT-28601-VAthmKFbIEYhzcGlyIrTg20jh51EU0LSrNn; CASTGC=TGT-22-Jk7yMyxkcL7bO1B1GxB5sOuUs3gmXnLPL0gNCAmCWC4bVro7Kl-cas; COOKIE_CURRENT_LOGIN_USER_STATUS=7970A7DF80FF7766A7587A2AF33EE5F23D5F28C44A1B6E376A7769EDE8868F6A4E35D84E60BEA850007111C033A037FB");
            //启动执行请求，并获得返回值
            CloseableHttpResponse response = client.execute(post);
            //得到返回的entity对象
            HttpEntity entity = response.getEntity();
            //把实体对象转换为string
            String result = EntityUtils.toString(entity, "UTF-8");
            //返回内容
            return result;
        } catch (Exception e1) {
            e1.printStackTrace();
            return null;
        }
    }

    /**
     * 获取除当前用户以外的用户列表
     *
     * @param userId 当前用户id
     * @return 用户列表
     */
    @GetMapping("getOtherUserInfoList")
    public AppReply getOtherUserInfoList(String userId, String userName) {
        return new AppReply(AppReply.SUCCESS_CODE, AppReply.SUCCESS_MSG, userDao.selectOtherUserInfoList(userId, userName));
    }
}
